import {message} from 'antd';
import {formatMessage} from './intl';
import {partOrderPriceTerms} from '../Enum';

export const newOrUpdateOrderValidator = (data, details) => {
    //增加判断必填字段
    const emptyErrors = [];
    if(!data || !data.dealerId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.dealerId',
            defaultMessage: '总代'
        }));
    if(!data || !data.brandId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brandId',
            defaultMessage: '品牌'
        }));
    if(!data || !data.partOrderTypeId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.partOrderType',
            defaultMessage: '订单类型'
        }));
    if(!data || !data.paymentType)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.paymentType',
            defaultMessage: '付款方式'
        }));
    if(!data || !data.partOrderPriceTerms)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.partOrderPriceTerms',
            defaultMessage: '价格条款'
        }));
    if(data && data.partOrderPriceTerms === partOrderPriceTerms.FCA && !data.account)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.account',
            defaultMessage: '到付账号'
        }));
    if(data && (data.partOrderPriceTerms === partOrderPriceTerms.FCA ||
        data.partOrderPriceTerms === partOrderPriceTerms.FOB ||
        data.partOrderPriceTerms === partOrderPriceTerms.EXW) && !data.carier)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.carier',
            defaultMessage: '指定货代'
        }));
    if(!data || !data.receivedCompany)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedCompany',
            defaultMessage: '收货方'
        }));
    if(!data || !data.receivedTelephone)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedTelephone',
            defaultMessage: '收货方电话'
        }));
    if(!data || !data.receivedPerson)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedPerson',
            defaultMessage: '收货联系人'
        }));
    if(!data || !data.receivedPersonPhone)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedPersonPhone',
            defaultMessage: '收货人联系电话'
        }));
    if(!data || !data.receivedAddress)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedAddress',
            defaultMessage: '收货地址'
        }));
    if(!data || !data.receivedPort)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.receivedPort',
            defaultMessage: '目的港口'
        }));
    if(!data || !data.transportType)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.transportType',
            defaultMessage: '运输方式'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    if(data && data.remark && data.remark.length > 100) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.empty.remark',
            defaultMessage: '备注长度不能超过100个字'
        })}`);
        return false;
    }
    if(details.length <= 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.detailRequired',
            defaultMessage: '清单不能为空'
        })}`);
        return false;
    }
    if(details.some(d => !d.planQuantity || d.planQuantity <= 0)) {
        message.warning(formatMessage({
            id: 'validator.message.planQuantity',
            defaultMessage: '订购数量不能为0！'
        }));
        return false;
    }
    if(details.some(d => d.isType === true)) {
        message.warning(formatMessage({
            id: 'validator.message.isType',
            defaultMessage: '备件的扩展属性不符合 备件销售订单类型不可提报备件属性'
        }));
        return false;
    }
    if(details.some(d => d.isTransport === true)) {
        message.warning(formatMessage({
            id: 'validator.message.isTransport',
            defaultMessage: '备件的扩展属性不符合 运输方式不可提报备件属性'
        }));
        return false;
    }
    for(let i = 0; i < details.length; i++) {
        const {planQuantity, minPackSize} = details[i];
        if(planQuantity && minPackSize && !Number.isInteger(planQuantity / minPackSize)) {
            message.warning(formatMessage({
                id: 'validator.message.planQuantityMinPackSize',
                defaultMessage: '备件最小包装数大于0时，备件清单的订货数量是最小包装数的整数倍'
            }));
            return false;
        }
    }
    return true;
};

export const rejectValidator = (data, details) => {
    //增加判断必填字段
    const emptyErrors = [];
    if(!data || !data.approvedContent)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.approvedContent',
            defaultMessage: '审核意见'
        }));
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    return true;
};
